package com.ehotting.edsta.mqbd.monitor.task.active.module;

import com.alibaba.fastjson.JSON;
import com.ehotting.eaf.core.utils.OkHttpUtils;
import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.edsta.mqbd.api.enums.SysModuleEnum;
import com.ehotting.mqbd.api.bean.solver.SolverStateBean;
import com.ehotting.mqbd.core.logger.LOGGER_KEY;
import com.ehotting.mqbd.core.logger.ModuleActiveLogger;
import com.ehotting.mqbd.core.monitor.ModuleActiveAble;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.List;

public class CAEUActive implements ModuleActiveAble {

    private static Logger logger = LoggerFactory.getLogger(CAEUActive.class);

    private ModuleActiveLogger mqActiveLogger;

    private static String URL = "/subtask/getThreadPoolStatus";

    public CAEUActive(ModuleActiveLogger mqActiveLogger) {
        this.mqActiveLogger = mqActiveLogger;
    }

    public ModuleActiveLogger getModuleActiveLogger() {
        return mqActiveLogger;
    }

    @Override
    public String getModule() {
        return SysModuleEnum.CAEU.getCode();
    }

    public  Integer doAction(SolverStateBean solverState){
        String requestUrl = "http://"+solverState.getSolverInfo().getIp()+":"+solverState.getSolverInfo().getPort()+ URL;
        MDC.put(LOGGER_KEY.URL,requestUrl);
        logger.info("requestUrl:{}",requestUrl);
        String resultString = OkHttpUtils.builder(5,5,5).url(requestUrl).addHeader("authorization"
                ,"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6InRlc3RlciIsInVzZXJJZCI6MTQyMzYxNjU0fQ.alyZgfFwavfZysfYwpYXuarY5ZtBFjsyAmTDfdHdu_A").post(false).sync();
        logger.info("result:{}",resultString);
        MDC.put(LOGGER_KEY.RESPONSE,resultString);
        return getUnFinish(resultString);
    }


    public  Integer getUnFinish(String resultString){
        if(StringUtils.isEmpty(resultString)){
            logger.error("resultString is null,return");
            return  -1;
        }
        Result result = JSON.parseObject(resultString, Result.class);
        if(result==null ){
            logger.error("result is null,return");
            return -1;
        }
        if(result.getCode()==null ){
            logger.error("result code is null,return");
            return -1;
        }
        if(result.getCode().equals(0)){
            return getUnFinish(result.getData());
        }
        return -1;
    }

    private static Integer getUnFinish(List<ResultData> data){
        Integer unStart = 0;
        Integer unFinish = 0 ;
        if(data!=null && data.size()>0){
            for(ResultData json : data){
                unFinish = json.getActive()+json.getQueue();
            }
        }
        return unStart + unFinish;
    }

    public static class Result{
        private Integer code;
        private String message;
        private List<ResultData> data;
        private String error;

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public List<ResultData> getData() {
            return data;
        }

        public void setData(List<ResultData> data) {
            this.data = data;
        }

        public String getError() {
            return error;
        }

        public void setError(String error) {
            this.error = error;
        }
    }

    public static class ResultData {

        private Integer active;
        private Integer queue;

        public Integer getActive() {
            return active;
        }

        public void setActive(Integer active) {
            this.active = active;
        }

        public Integer getQueue() {
            return queue;
        }

        public void setQueue(Integer queue) {
            this.queue = queue;
        }
    }


}
